﻿using Student2024.Model.EF;
using Student2024.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Student2024.Service
{
    internal class StudentSystemService
    {
        /// <summary>
        /// 获取所有学生信息记录
        /// </summary>
        /// <returns></returns>
        public List<T001Student> GetAll()
        {
            var db = new MyNewSystemEntities1();
            return db.T001Student.ToList();
        }

        /// <summary>
        /// 根据考试类型获取学生记录
        /// </summary>
        /// <param name="examType"></param>
        /// <returns></returns>
        public List<T001Student> GetAllByExamType(string examType)
        {
            var db = new MyNewSystemEntities1();
            return db.T001Student.Where(m => m.ExamType == examType).ToList();
        }

        /// <summary>
        /// 创建学生记录
        /// </summary>
        /// <param name="model"></param>
        public void Create(T001StudentCreateModel model)
        {
            //连接数据库结构
            var db = new MyNewSystemEntities1();
            //新增数据方法
            var newItem = new T001Student()
            {
                Id = Guid.NewGuid(),
                Name = model.Name,
                Province = model.Province,
                ExamType = model.ExamType,
                ObjectGroup = model.ObjectGroup,
                SchoolName = model.SchoolName,
                PhoneNum = model.PhoneNum,
                Genger = model.Gender,
            };
            //新增数据记录
            db.T001Student.Add(newItem);
            //保存
            db.SaveChanges();
        }

        /// <summary>
        /// 根据id获取学生记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T001Student GetById(Guid id)
        {
            var db = new MyNewSystemEntities1();
            return db.T001Student.Find(id);
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="model"></param>
        public void Edit(T001StudentEditModel model)
        {
            var db = new MyNewSystemEntities1();
            //把items和model连接赋值
            var items = db.T001Student.Find(model.Id);
            if (items != null)
            {
                //修改数据内容
                items.Name = model.Name;
                items.Province = model.Province;
                items.ObjectGroup = model.ObjectGroup;
                items.SchoolName = model.SchoolName;
                items.PhoneNum = model.PhoneNum;
                items.Genger = model.Genger;

            };
            //保存修改
            db.SaveChanges();
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(Guid id)
        {
            var db = new MyNewSystemEntities1();
            var item = db.T001Student.Find(id);
            if (item != null)
            {
                db.T001Student.Remove(item);
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 返回指定考试类型中符合要求的学生记录
        /// </summary>
        /// <param name="examType"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static List<T001Student> GetAllByExamTypeAndSearch(string examType, string Name)
        {
            var db = new MyNewSystemEntities1();
            return db.T001Student.Where(m => m.ExamType == examType && m.Name.Contains(Name)).ToList();
        }
    }
}
